<template>
  <div>
    <el-dialog
      title="权限管理(一级为页面查看权限,二级为页面操作权限)"
      :visible="showPermissionDialog"
      width="50%"
      @close="closeDialog"
    >
      <el-tree
        ref="tree"
        :show-checkbox="true"
        check-strictly
        node-key="id"
        :data="PermissionList"
        :props="{ label: 'name' }"
      ></el-tree>

      <span slot="footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionListAPI } from '@/api/permisson'
import { getRoleDetailAPI, updatePermissionByIdAPI } from '@/api/setting'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'PermissionContrail',
  props: {
    showPermissionDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      PermissionList: [], // 树形结构
      roleID: '' // 员工ID

    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    // 关闭弹出层
    closeDialog () {
      this.$emit('update:showPermissionDialog', false)
    },
    // 调用获取树形结构的API
    async getPermissionList () {
      const data = await getPermissionListAPI() // 无需传入参数,垃圾文档
      this.PermissionList = tranListToTreeData(data) // 获取到树形结构
    },
    // 调用获取员工详情的API(回填数据)
    async getRoleDetail(id) {
      // 从父组件获取到员工id
      this.roleID = id
      const res = await getRoleDetailAPI(id)
      console.log(res)
      // 回填操作
      this.$refs.tree.setCheckedKeys(res.permIds)
    },
    // 点击确定调用更新权限的API
    async onSubmit() {
      this.$refs.tree.getCheckedKeys() // 获取到点击的权限的那一项所代表的id
      console.log(this.$refs.tree.getCheckedKeys())

      await updatePermissionByIdAPI({ id: this.roleID, permIds: this.$refs.tree.getCheckedKeys() })
      this.$message.success('修改权限成功')
      this.$emit('update:showPermissionDialog', false)
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
